<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tdma.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_tdma.js.html">tdma.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview The Tridiagonal matrix algorithm solver solves a special
<a name="line17"></a> * version of a sparse linear system Ax = b where A is tridiagonal.
<a name="line18"></a> *
<a name="line19"></a> * See http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.math.tdma&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Solves a linear system where the matrix is square tri-diagonal. That is,
<a name="line28"></a> * given a system of equations:
<a name="line29"></a> *
<a name="line30"></a> * A * result = vecRight,
<a name="line31"></a> *
<a name="line32"></a> * this class computes result = inv(A) * vecRight, where A has the special form
<a name="line33"></a> * of a tri-diagonal matrix:
<a name="line34"></a> *
<a name="line35"></a> *    |dia(0) sup(0)   0    0     ...   0|
<a name="line36"></a> *    |sub(0) dia(1) sup(1) 0     ...   0|
<a name="line37"></a> * A =|                ...               |
<a name="line38"></a> *    |0 ... 0 sub(n-2) dia(n-1) sup(n-1)|
<a name="line39"></a> *    |0 ... 0    0     sub(n-1)   dia(n)|
<a name="line40"></a> *
<a name="line41"></a> * @param {!Array.&lt;number&gt;} subDiag The sub diagonal of the matrix.
<a name="line42"></a> * @param {!Array.&lt;number&gt;} mainDiag The main diagonal of the matrix.
<a name="line43"></a> * @param {!Array.&lt;number&gt;} supDiag The super diagonal of the matrix.
<a name="line44"></a> * @param {!Array.&lt;number&gt;} vecRight The right vector of the system
<a name="line45"></a> *     of equations.
<a name="line46"></a> * @param {Array.&lt;number&gt;=} opt_result The optional array to store the result.
<a name="line47"></a> * @return {!Array.&lt;number&gt;} The vector that is the solution to the system.
<a name="line48"></a> */
<a name="line49"></a>goog.math.tdma.solve = function(
<a name="line50"></a>    subDiag, mainDiag, supDiag, vecRight, opt_result) {
<a name="line51"></a>  // Make a local copy of the main diagonal and the right vector.
<a name="line52"></a>  mainDiag = mainDiag.slice();
<a name="line53"></a>  vecRight = vecRight.slice();
<a name="line54"></a>
<a name="line55"></a>  // The dimension of the matrix.
<a name="line56"></a>  var nDim = mainDiag.length;
<a name="line57"></a>
<a name="line58"></a>  // Construct a modified linear system of equations with the same solution
<a name="line59"></a>  // as the input one.
<a name="line60"></a>  for (var i = 1; i &lt; nDim; ++i) {
<a name="line61"></a>    var m = subDiag[i - 1] / mainDiag[i - 1];
<a name="line62"></a>    mainDiag[i] = mainDiag[i] - m * supDiag[i - 1];
<a name="line63"></a>    vecRight[i] = vecRight[i] - m * vecRight[i - 1];
<a name="line64"></a>  }
<a name="line65"></a>
<a name="line66"></a>  // Solve the new system of equations by simple back-substitution.
<a name="line67"></a>  var result = opt_result || new Array(vecRight.length);
<a name="line68"></a>  result[nDim - 1] = vecRight[nDim - 1] / mainDiag[nDim - 1];
<a name="line69"></a>  for (i = nDim - 2; i &gt;= 0; --i) {
<a name="line70"></a>    result[i] = (vecRight[i] - supDiag[i] * result[i + 1]) / mainDiag[i];
<a name="line71"></a>  }
<a name="line72"></a>  return result;
<a name="line73"></a>};
</pre>


</body>
</html>
